﻿using log4net;
using log4net.Config;
using log4net.Repository;
using Microsoft.Extensions.DependencyInjection;
using SimpleCrawler.DAL;
using SimpleCrawler.Tools;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

namespace SimpleCrawler.Extensions
{
    public static class BloomFilterServiceCollectionExtensions
    {
        public static IServiceCollection AddBloomFilterServiceCollectionExtensions(this IServiceCollection serviceCollection)
        {
            CrawlerDAL dal = serviceCollection.BuildServiceProvider().GetService<CrawlerDAL>();
            //dal
            DataTable dt = dal.GetAllUrl().Result.Tables[0];
            int capacity = dt.Rows.Count;
            if (capacity > 0)
            {
                var filter = new BloomFilter<string>(capacity);
                foreach (DataRow row in dt.Rows)
                {
                    filter.Add(row[0].ToString());
                }
                serviceCollection.AddSingleton<BloomFilter<string>>(filter);
            }
            //
            return serviceCollection;
        }
    }
}
